package main

import (
	"fmt"
)

func main() {
	//initSum(1, 2)
	//initSum2(1, 3, 2, 23, 23, 31, 231)
	//c = add
	//fmt.Printf("%d",c(1,2))
	//f1, f2 := calc(10)
	//fmt.Println(f1(1),f2(2))
	//fmt.Println(f1(3),f2(4))
	//fmt.Println(f1(5),f2(6))
	//fmt.Println(f1())
	//fmt.Println(f2())
	//fmt.Println(f3())
	//fmt.Println(f4())
	//testCalc()
	//funcA()
	//funcB()
	//funcC()
	left := dispatchCoin(coins, users, distribution)
	left2 := dispatchCoin2()
	fmt.Println("left:", left)
	fmt.Println("left:", left2)
}

//func initSum(x int, y int) int {
//	return x + y
//}
//func initSum(x, y int) int {
//	return x + y
//}
//func initSum2(x ...int) int {
//	fmt.Println(x)
//	sum := 0
//	for _, v := range x {
//		sum += v
//	}
//	return sum
//}

//func calc(x,y int) (int, int) {
//	sum:=x+y
//	sub:=x-y
//	return sum, sub
//}
//func clac(x, y int) (sum, sub int) {
//	sum = x + y
//	sub = x - y
//	return
//}
//func someFunc(x string) []int {
//	if x== "" {
//		return nil
//	}
//	return []int{}
//}
//func do(s string) (func(int, int) int, error) {
//	switch s {
//	case "+":
//		return add, nil
//	case "-":
//		return sub, nil
//	default:
//		err := errors.New("无法识别的操作符")
//		return nil, err
//	}
//}
//type calculation func(int, int) int
//
//func add(x, y int) int {
//	return x + y
//}
//func sub(x, y int) int {
//	return x - y
//}
//var c calculation
//func calc (x,y int,op func(int,int) int) int {
//	return op(x, y)
//}
//func calc(base int) (func(int) int, func(int) int) {
//	add := func(i int) int {
//		base += i
//		return base
//	}
//	sub := func(i int) int {
//		base -= i
//		return base
//	}
//	return add, sub
//}
func f1() int {
	x := 5
	defer func() {
		x++
	}()
	return x //5
}
func f2() (x int) {
	defer func() {
		x++
	}()
	return 5 //6
}
func f3() (y int) {
	x := 5
	defer func(x int) {
		x++
	}(x)
	return x //5
}
func f4() (x int) {
	defer func(x int) {
		x++
	}(x)
	return 5 //5
}
func calc(index string, a, b int) int {
	ret := a + b
	fmt.Println(index, a, b, ret)
	return ret
}
func testCalc() {
	x := 1
	y := 2
	defer calc("aa", x, calc("a", x, y))
	x = 10
	defer calc("bb", x, calc("b", x, y))
	y = 20
}
func funcA() {
	fmt.Println("funcA")
}
func funcB() {
	defer func() {
		err := recover()
		fmt.Println(err)
	}()
	panic("panic in B")
}
func funcC() {
	fmt.Println("funcC")
}

/*
你有50枚金币，需要分配给以下几个人：Matthew,Sarah,Augustus,Heidi,Emilie,Peter,Giana,Adriano,Aaron,Elizabeth。
分配规则如下：
a. 名字中每包含1个'e'或'E'分1枚金币
b. 名字中每包含1个'i'或'I'分2枚金币
c. 名字中每包含1个'o'或'O'分3枚金币
d: 名字中每包含1个'u'或'U'分4枚金币
写一个程序，计算每个用户分到多少金币，以及最后剩余多少金币？
程序结构如下，请实现 ‘dispatchCoin’ 函数
*/
var (
	coins = 50
	users = []string{
		"Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth",
	}
	distribution = make(map[string]int, len(users))
)

func dispatchCoin(coins int, users []string, distribution map[string]int) int {
	const (
		E = 1
		I = 2
		O = 3
		U = 4
	)
	for _, v := range users {
		distribution[v] = 0
		for _, r := range v {
			switch string(r) {
			case "e", "E":
				distribution[v] += E
			case "i", "I":
				distribution[v] += I
			case "o", "O":
				distribution[v] += O
			case "u", "U":
				distribution[v] += U
			}
		}
		coins -= distribution[v]
	}
	fmt.Printf("%v\n", distribution)
	return coins
}
func dispatchCoin2() (left int) {
	var (
		coins = 50
		users = []string{
			"Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth",
		}
		distribution = make(map[string]int, len(users))
	)
	var a = 'H'
	fmt.Printf("%T\n", a)
	for _, name := range users {
		getCoin := 0
		for _, n := range name {
			switch n {
			case 'e', 'E':
				getCoin += 1
			case 'i', 'I':
				getCoin += 2
			case 'o', 'O':
				getCoin += 3
			case 'u', 'U':
				getCoin += 4
			}
		}
		distribution[name] = getCoin
		coins -= getCoin
	}
	fmt.Println(distribution)
	return coins
}
